<template>
  <div class="member-order">
    <!-- tab组件 -->
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel v-for="item in orderStatus" :key="item.name" :label="item.label" :name="item.name"></XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        @on-cancel="handlerCancel"
        @on-delete-order="handlerDelete"
        @on-confirm="handlerConfirm"
        @on-logistics="handlerLogistics"
        v-for="item in orderList"
        :key="item.id"
        :order="item"
       />
    </div>
    <!-- 分页组件 -->
    <xtxPagination
      v-if="total > 0"
      :current-page="reqParams.page"
      :page-size="reqParams.pageSize"
      :total="total"
      @current-change="reqParams.page=$event"
    />
    <!-- 取消原因组件 -->
    <OrderCancel ref="orderCancelCom" />
     <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogisticslTag" />
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constant'
import OrderItem from './components/order-item.vue'
import { deleteOrder, findOrderList, confirmReceipt } from '@/api/order'
import OrderCancel from './components/order-cancel.vue'
import OrderLogistics from './components/order-logistics.vue'
import Message from '@/components/library/message'
import Confirm from '@/components/library/confirm'
export default {
  name: 'MemberOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup () {
    const activeName = ref('all')
    // 点击选项卡
    const tabClick = ({ index }) => {
      reqParams.page = 1
      reqParams.orderState = index
    }
    // 查询订单参数
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 获取订单列表
    const orderList = ref([])
    const loading = ref(false)
    // 分页需要的数据总条数
    const total = ref(0)
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    // 切换分页时发生变化就重新加载数据
    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })
    // 删除订单
    const handlerDelete = (order) => {
      Confirm({ text: '亲，您确认删除该条订单吗？' }).then(() => {
        deleteOrder(order.id).then(() => {
          Message({ type: 'success', text: '删除订单成功' })
          // 重新获取订单列表
          getOrderList()
        })
      }).catch(e => {})
    }
    return { activeName, orderStatus, tabClick, orderList, loading, reqParams, total, handlerDelete, ...useCancel(), ...useConfirm(), ...useLogistics() }
  }
}
// 取消订单逻辑 从setup中拆出来，然后到setup中去解构
export const useCancel = () => {
  // 拿到取消订单组件实例
  const orderCancelCom = ref(null)
  // 点击取消
  const handlerCancel = (order) => {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}
// 确认收货处理逻辑
export const useConfirm = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '您确认收货吗？确认后钱款将立即打给卖家' }).then(() => {
      confirmReceipt(order.id).then(() => {
        Message({ type: 'success', text: '确认收货成功' })
        // 订单状态此时更新为 待评价4
        order.orderState = 4
      })
    }).catch(() => {})
  }
  return { handlerConfirm }
}
// 查看物流处理逻辑 (此方法可导出，在订单详情里查看物流状态可以直接调用此方法)
export const useLogistics = () => {
  // 拿到物流组件实例
  const orderLogisticslTag = ref(null)
  const handlerLogistics = (order) => {
    // 打开查看物流的对话框
    orderLogisticslTag.value.open(order)
  }
  return { handlerLogistics, orderLogisticslTag }
}
</script>

<style lang="less" scoped>
.member-order {
  height: 100%;
  background: #fff;
}
.order-list {
  padding: 20px;
  min-height: 400px;
  position: relative;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
